#ifndef ACEMESH_UTILS
#define ACEMESH_UTILS

#include <stddef.h>
#include <time.h>

#include "task.h"
long long tick_count_now();

unsigned long rpcc();
unsigned long rtc();
// extern int total_num_threads;
// extern int cgid;
// #ifdef GS_AGENT
// #define thread_id 0
// #else
// #define thread_id total_num_threads-1
// #endif

/** Defined in tbb_main.cpp **/
unsigned GetPrime(unsigned seed);

#define mymax(a, b) ((a > b) ? (a) : (b))
#define mymin(a, b) ((a < b) ? (a) : (b))

//! A fast random number generator.
/** Uses linear congruential method. */
/*class FastRandom {
    unsigned x, a;
public:
    //! Get a random number.
    unsigned short get() {
        return get(x);
    }
    //! Get a random number for the given seed; update the seed for next use.
    unsigned short get( unsigned& seed ) {
        unsigned short r = (unsigned short)(seed>>16);
        seed = seed*a+1;
        return r;
    }
    //! Construct a random number generator.
    FastRandom( unsigned seed ) {
        x = seed;
        a = GetPrime( seed );
    }
};*/
//}

#ifndef DISTRIBUTED_SCHEDULER
#define MS_SCHED 1
#define SL_SCHED 0
#endif

#ifdef ACEMESH_GRAPH_BUILD_PROFILING
#define N_BUILD_PROF 29
#define T_GRAPH_BUILD 0  //
#define T_GRAPH_EXEC 1   //
#define T_GRAPH_TIME 2   //
#define BEGIN_SPLIT 3
#define ARGS_TRANS 4
#define TASK_CONSTR 5  //
#define TASK_GENE 6    //
#define REG_TASK_IN 7  //
#define END_TASKS_L 8
#define BUILD_RELAT 9
#define BUILD_RELAT_IN 10
#define ADD_SUCC 11
#define DEL_END_L 12
#define IF_CON 13
#define FIND_HASH 14
#define INSERT_HASH 15
#define TRAVE_LIST 16
#define NEED_SPAWN_P 17
#define VEC_CLEAR 18
#define END_SPLIT 19
#define UPDATE_HASH 20     //
#define UPDATE_ITEM_IN 21  //
#define FIND_HASH2 22      //
#define UPDATE_DATA 23     //
#define CLEAR_NEED_SPAWN 24
#define GET_END_LIST 25
#define JOIN_TASK 26    //
#define RESET_GRAPH 27  //
#define RESTROE_GRAPH 28
#endif
#define MULTI_TASK 512
#define BLOCK_SIZE 2048
#define MULTI_STATUS 16
#ifdef __cplusplus
extern "C" {
#ifdef TARG_SW9
void my_atomic_add(volatile int *ref);
#ifndef MULTI_SPAWN
void my_spawn_to_id(struct task *t);
#else
void my_spawn_to_id(int num);
#endif
void my_atomic_sub(volatile int *ref);
void my_add_successor(struct aceMesh_task *preTask);
void my_add_end_successor(struct aceMesh_task *preTask);
// void my_send_to_agent(struct aceMesh_task *dest, volatile int num);
#endif
#endif
void my_mutex_lock(volatile int *my_lock);
void my_mutex_unlock(volatile int *my_lock);
#ifdef __cplusplus
}
#endif

void slave_my_mutex_lock(volatile int *my_lock);
void slave_my_mutex_unlock(volatile int *my_lock);

#endif
